package com.bydq.patrolsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bydq.patrolsystem.entity.PsFunction;
import com.bydq.patrolsystem.mapper.PsFunctionMapper;
import com.bydq.patrolsystem.service.PsFunctionService;
import com.bydq.patrolsystem.vo.FunctionTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 写代码是不可能写代码的啦~~
 * @since 2020-05-22
 */
@Service
public class PsFunctionServiceImpl extends ServiceImpl<PsFunctionMapper, PsFunction> implements PsFunctionService {


    @Autowired
    private PsFunctionMapper psFunctionMapper;


    @Override
    public List<PsFunction> listParent() {
        return psFunctionMapper.selectParent();
    }

    @Override
    public List<FunctionTreeVo> functionPoToVo(List<PsFunction> psFunctions) {
        //创建一个vo的集合
        List<FunctionTreeVo> functionTreeVos = new ArrayList<>();

        //转化父节点
        for (PsFunction psFunction : psFunctions) {
            //一个功能的转换过程
            FunctionTreeVo functionTreeVo = new FunctionTreeVo(psFunction.getFunctionName(), psFunction.getId(), eachChilds(psFunction.getChildren()), psFunction.getIsStart() == 0);
            functionTreeVos.add(functionTreeVo);
        }

        return functionTreeVos;
    }

    @Transactional
    @Override
    public boolean delFunctionAndChildren(Integer id) {
            //获取当前id的节点
            PsFunction psFunction = psFunctionMapper.selectById(id);

            //先获取子节点
            List<PsFunction> children = psFunction.getChildren();

            //把所有子节点引用的父节点设置为-1
            eachDel(children);

            //删除 子孙节点
            psFunctionMapper.delete(new QueryWrapper<PsFunction>().eq("parentId", -1));

            //删除当前节点
            psFunctionMapper.deleteById(id);


        return true;
    }

    //递归所有子节点删除
    private void eachDel(List<PsFunction> children) {
        if(children!=null) {
            for (PsFunction child : children) {
                //更新当前节点的parentid为-1
                child.setParentId(-1);
                psFunctionMapper.updateById(child);
                eachDel(child.getChildren());
            }
        }
    }


    //递归当前的父节点的所有子节点 返回一个转换后的子节点集合
    private List<FunctionTreeVo> eachChilds(List<PsFunction> psFunctions) {
        ArrayList<FunctionTreeVo> treeVos = new ArrayList<>();
        //如果不为空则说明还有子节点 则继续遍历
        if(psFunctions!=null){
//            遍历所有的子节点
            for (PsFunction function : psFunctions) {
                //将当前子节点转化
                FunctionTreeVo functionTreeVo = new FunctionTreeVo(function.getFunctionName(), function.getId(), eachChilds(function.getChildren()), function.getIsStart() == 0);
                //添加到容器中
                treeVos.add(functionTreeVo);
            }
        }
        //返回转化后的子节点集合
        return treeVos;
    }
}
